package main

import (
	"flag"
	"go-gateway-learn/dao"
	http_proxy_router "go-gateway-learn/httpproxyrouter"
	"go-gateway-learn/router"
	tcpproxyrouter "go-gateway-learn/tcp_proxy_router"
	"os"
	"os/signal"
	"syscall"

	"go-gateway-learn/golang_common/lib"
)

var (
	endpoint   = flag.String("endpoint", "server", "input endpoint name: dashboard or server")
	mainConfig = flag.String("configpath", "./conf/dev/", "input config file : ./conf/dev/")
)

func main() {
	flag.Parse()
	if *endpoint == "" || *mainConfig == "" {
		flag.Usage()
		os.Exit(1)
	}
	if *endpoint == "dashboard" {
		lib.InitModule(*mainConfig, []string{"base", "mysql", "redis"})
		defer lib.Destroy()
		router.HttpServerRun()

		quit := make(chan os.Signal, 1)
		signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
		<-quit

		router.HttpServerStop()
	} else {
		lib.InitModule(*mainConfig, []string{"base", "mysql", "redis", "proxy"})
		defer lib.Destroy()
		dao.ServiceManagerHandler.LoadOnce()
		dao.AppManagerHandler.LoadOnce()
		go http_proxy_router.HttpProxyRun()
		go http_proxy_router.HttpsProxyRun()
		go tcpproxyrouter.TcpServeRun()

		quit := make(chan os.Signal, 1)
		signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
		<-quit
		
		http_proxy_router.HttpProxyStop()
		http_proxy_router.HttpsProxyStop()
		tcpproxyrouter.TCPServerStop()
	}

}
